package com.wy.controller;

import com.wy.config.XXConfig;
import com.wy.domain.User;
import com.wy.jparepo.UserRepository;
import com.wy.jparepo.UserService;
import com.wy.mongo.MongoUtil;
import com.wy.mybatisrepo.UserMapper;
import com.wy.mybatisrepo.UserMybatisService;
import com.wy.redis.RedisUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
public class UserController {
    @Autowired
    private UserRepository userRepository;

    @Autowired
    private UserMybatisService mybatisService;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private XXConfig config;

    @Autowired
    private UserService userService;

    @Autowired
    private RedisUtil util;

    @Autowired
    private MongoUtil mongoUtil;

    @GetMapping("/hello")
    public String hello(){
        return "hello world";
    }

//    @GetMapping("/listjpa")
//    public List<User> findAll(){
//       return userRepository.findAll();
//    }

    @GetMapping("/listmybatis")
    public List<User> findAllMybatis(){
       return mybatisService.findAll();
    }

    @GetMapping("/findbypage")
    public PageInfo<User> findByPage(@RequestParam(value = "pageNo", defaultValue = "1") int pageNo, @RequestParam(value = "pageSize",defaultValue = "2") int pageSize){
        PageHelper.startPage(pageNo, pageSize);
        Page<User> byPage = userMapper.findByPage();
        PageInfo<User> pageInfo = new PageInfo<>(byPage);

        return pageInfo;
    }

    @GetMapping("/findbypagejpa")
    public org.springframework.data.domain.Page<User> findByPageJpa(@RequestParam(value = "pageNo", defaultValue = "1") int pageNo, @RequestParam(value = "pageSize",defaultValue = "2") int pageSize){
        org.springframework.data.domain.Page<User> byPage = userService.findByPage(pageNo, pageSize);

        return byPage;
    }

    @GetMapping("/config")
    public String getConfig(){
        log.debug("请求来啦~~~~~");
        return config.getName()+config.getAge();
    }

    @GetMapping("/getredis")
    public String getRedis(){
        util.save("keytest","valuetest");
        return util.get("keytest");
    }

    @GetMapping("/mongo")
    public User mongo(){
        User user = new User();
        user.setName("wangyong");
        user.setAge(18);
        user.setId(1);

        mongoUtil.save(user);

        return mongoUtil.findByUserName("wangyong");

    }

    @GetMapping("/mongo/delete")
    public String mongoDelete(){
        mongoUtil.delete();
        return "succ";
    }

}
